//
//  BKUserFindPwdNextViewController.m
//  bestkeep
//
//  Created by windCould on 16/11/29.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserFindPwdNextViewController.h"
#import "BKMyCommon.h"
#import "BKCustextFieldView.h"
#import "BKUserUnitRequestAPI.h"
#import "BKUserCodeFormat.h"
#import "BKUserRetirePwdController.h"

@interface BKUserFindPwdNextViewController ()<BKCustextFieldViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong)BKCustextFieldView * countDownTexField;
@property (nonatomic,strong)UIButton * nextBtn;


@end

@implementation BKUserFindPwdNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.title = @"找回密码";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpViews];
    _countDownTexField.countDownBtn.btnSelect = YES;
}


#pragma mark =========================BKCustextFieldViewDelegate=================

-(void)custextFieldView:(BKCustextFieldView *)custextFieldView rightButtonTouch:(BKUserCustomBtn *)btn
{
    [BKIndicationView showInView:self.view];
    [BKUserUnitRequestAPI findPwdGetSMSCodeStrWithAccount:self.accountStr resultBlock:^(id obj, NSError *error) {
        [BKIndicationView dismiss];
        if (obj) {
            
            BKUserRequestResultModel * result = obj;
            if ([result.success isEqualToString:@"1"]) {
                
                NSLog(@"%@",result.msg);
                [HUD showSuccessMessage:result.msg];
            } else {
            
                NSLog(@"%@",result.msg);
                btn.btnSelect = NO;
                [HUD showSuccessMessage:result.msg];
            }
            
        } else {
        
            btn.btnSelect = NO;
            [HUD showErrorMessage:@"网络请求失败"];
        }
        
    }];
}


#pragma mark =========================Function===================================

-(void)goToNext
{
    if (![BKUserCodeFormat parameterValueIsNotNilAndZeroLengthWithContentStr:self.countDownTexField.textField.text]) {
        
        NSLog(@"验证码不能为空。。。。。。");
        [HUD showErrorMessage:@"验证码不能为空"];
        return;
    }
    
    
    __weak typeof(self) weakSelf = self;
    
    [BKUserUnitRequestAPI verifySMSVerificationcodeWithCodeStr:self.countDownTexField.textField.text accountNum:self.accountStr resultBlock:^(id obj, NSError *error) {
        
        if (obj) {
            
            BKUserRequestResultModel * result = obj;
            if ([result.success isEqualToString:@"1"]) {
                
                BKUserRetirePwdController * retirePwdVc = [[BKUserRetirePwdController alloc]init];
                retirePwdVc.phoneNumber = weakSelf.accountStr;
                retirePwdVc.passData = result.code;
                [self.navigationController pushViewController:retirePwdVc animated:YES];
                NSLog(@"%@",result.msg);
               // [HUD showSuccessMessage:result.msg];
                
            } else {
                
                NSLog(@"%@",result.msg);
                [HUD showErrorMessage:@"验证码不正确"];
            }
            
            
        } else {
            
            NSLog(@"网络请求失败。。。。。。");
            [HUD showErrorMessage:@"网络请求失败"];
        }
        
    }];
    
}


#pragma mark =========================布局========================================
-(void)setUpViews{

    _countDownTexField = [[BKCustextFieldView alloc]initWithFrame:CGRectZero style:BKCustextFieldViewTimeCountDownType];
    _countDownTexField.myDlegate = self;
    _countDownTexField.textField.delegate = self;
    _countDownTexField.placeHolderString = @"请输入短信验证码";
    _countDownTexField.imgUrlStr = @"BK_My_moblie_Message";
    [self.view addSubview:_countDownTexField];
    
    _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextBtn.titleLabel.font = UIFontBoldCustom(18);
    [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    _nextBtn.layer.cornerRadius = 4;
    _nextBtn.layer.masksToBounds = YES;
    [_nextBtn addTarget:self action:@selector(goToNext) forControlEvents:UIControlEventTouchUpInside];
    _nextBtn.backgroundColor = BK_MY_COLOR_01;
    [self.view addSubview:_nextBtn];
    [self layoutviews];
}


-(void)layoutviews
{
    [_countDownTexField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(@(50));
    }];
    
    
   [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
       make.top.mas_equalTo(_countDownTexField.mas_bottom).offset(20);
       make.left.mas_equalTo(self.view.mas_left).offset(20);
       make.right.mas_equalTo(self.view.mas_right).offset(-20);
       make.height.mas_equalTo(@(45));
    
   }];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
